function geoloc(map){
  this.map = map;
  	this.track = false;
  	this.listeners = [];
  	this.sucessCallback = [];
  	this.errorCallback = [];
  	this._canLocate = true;
  	this._defaultTimeout = 4000; // miliseconds
  	this._timeout = false;
  this.icons = [
    new G.MarkerImage("http://"+"www.gooltracking.com"+"/imas/main_images/loc_0.png", new G.Size(22,22), null, new G.Point(11,11)),
    new G.MarkerImage("http://"+"www.gooltracking.com"+"/imas/main_images/loc_1.png", new G.Size(22,22), null, new G.Point(11,11)),
    new G.MarkerImage("http://"+"www.gooltracking.com"+"/imas/main_images/loc_2.png", new G.Size(22,22), null, new G.Point(11,11)),
    new G.MarkerImage("http://"+"www.gooltracking.com"+"/imas/main_images/loc_3.png", new G.Size(22,22), null, new G.Point(11,11))
  ];
  
      if (navigator.geolocation) {
	      var that = this;
	      navigator.geolocation.getCurrentPosition( //Fa que li aparegui a l'usuari si permet geolocalitzar
		      function(p) {
			      that._canLocate = true;
			      that.marker = new G.Marker({icon:that.icons[0]}); //El primer cop, creem el marker
// 				that.marker.setIcon("/imas/main_images/loc.gif");
			      //L'icona animada la hem d'anmimar per codi, coses de "qualitat" y tal
			      that.marker.count = 0;
			      setInterval(function(){
				      if(that.marker.count > 3) that.marker.count = 0;
				      that.marker.setIcon(that.icons[that.marker.count]);
				      that.marker.count++;
			      },400);
			      that.marker.setMap(that.map);
			      var pos = new G.LatLng(p.coords.latitude,p.coords.longitude);
			      that.marker.setPosition(pos);
			      for(var callb in that.sucessCallback) that.sucessCallback[callb](pos);
			      that._cleanSuccess();
			      navigator.geolocation.watchPosition( //Iniciem polling
				      function(p) {
					      that._canLocate = true;
					      var pos = new G.LatLng(p.coords.latitude,p.coords.longitude);
					      that.marker.setPosition(pos);
					      if (that.track && that.map) {
						      that.map.setOptions({draggable:false});
						      that.map.panTo(pos);
					      }
					      for (var l in that.listeners) that.listeners[l](pos);
				      },
				      function(e) {
					      //Error: No s'ha pogut geolocalitzar (per exemple: no hi ha cobertura gps)
					      that._canLocate = false;
					      that._goError(e);
				      },
				      { enableHighAccuracy:true }
			      );
		      },
		      function(e) {
			      //Error: L'usuari no permet la geolocalitzacio
			      that._canLocate = false;
			      that._goError();
		      },
		      { enableHighAccuracy:true }
	      );
      }
}

geoloc.prototype._goError = function(e) {
      clearTimeout(this.timeout);
      for(var callb in this.errorCallback){ this.errorCallback[callb](e);}
      this.errorCallback = [];
}
geoloc.prototype._cleanSuccess = function() {
    clearTimeout(this.timeout);
    this.sucessCallback = [];
}
geoloc.prototype._try = function() {
    if(!navigator.geolocation){
	    this._canLocate = false;
	    this._goError();
	    return false;
    }
    var that = this;
    navigator.geolocation.getCurrentPosition( //Fa que li aparegui a l'usuari si permet geolocalitzar
	    function(p) {
		    that._canLocate = true;
		    var pos = new G.LatLng(p.coords.latitude,p.coords.longitude);
		    for(var callb in that.sucessCallback) that.sucessCallback[callb](pos);
		    that._cleanSuccess();
	    },
	    function(e) {
		    //Error: No s'ha pogut geolocalitzar (per exemple: no hi ha cobertura gps)
		    that._canLocate = false;
		    that._goError(e);
	    },
	    { enableHighAccuracy:true }
    );
}

// function GTgoloc(mapa) {
// 	this.map = mapa;
// 	this.track = false;
// 	this.listeners = [];
// 	this.sucessCallback = [];
// 	this.errorCallback = [];
// 	this._canLocate = true;
// 	this._defaultTimeout = 4000; // miliseconds
// 	this._timeout = false;
// 	this.icons = [
// 		new G.MarkerImage("http://"+"www.gooltracking.com"+"/imas/main_images/loc_0.png", new G.Size(22,22), null, new G.Point(11,11)),
// 		new G.MarkerImage("http://"+"www.gooltracking.com"+"/imas/main_images/loc_1.png", new G.Size(22,22), null, new G.Point(11,11)),
// 		new G.MarkerImage("http://"+"www.gooltracking.com"+"/imas/main_images/loc_2.png", new G.Size(22,22), null, new G.Point(11,11)),
// 		new G.MarkerImage("http://"+"www.gooltracking.com"+"/imas/main_images/loc_3.png", new G.Size(22,22), null, new G.Point(11,11))
// 	]
// 	var toast = new GToverMapElement(mapa, 'div', G.ControlPosition.BOTTOM_CENTER);
// 	toast.getDiv().className="toast";
// 	var that = this;
// 	this.showToast = function(t) {
// 		toast.getDiv().innerHTML = t;
// 		toast.render();
// 		if (that.toasttimmer) clearTimeout(that.toasttimmer);
// 		that.toasttimmer = setTimeout(function() {
// 			toast.hide(); 
// 			that.toasttimmer = false;
// 		}, 1500);
// 	}
// 	if (navigator.geolocation) {
// 		var that = this;
// 		navigator.geolocation.getCurrentPosition( //Fa que li aparegui a l'usuari si permet geolocalitzar
// 			function(p) {
// 				that._canLocate = true;
// 				that.marker = new G.Marker(); //El primer cop, creem el marker
// // 				that.marker.setIcon("/imas/main_images/loc.gif");
// 				//L'icona animada la hem d'anmimar per codi, coses de "qualitat" y tal
// 				that.marker.count = 0;
// 				setInterval(function(){
// 					if(that.marker.count > 3) that.marker.count = 0;
// 					that.marker.setIcon(that.icons[that.marker.count]);
// 					that.marker.count++;
// 				},400);
// 				that.marker.setMap(that.map);
// 				var pos = new G.LatLng(p.coords.latitude,p.coords.longitude);
// 				that.marker.setPosition(pos);
// 				for(var callb in that.sucessCallback) that.sucessCallback[callb](pos);
// 				that._cleanSuccess();
// 				navigator.geolocation.watchPosition( //Iniciem polling
// 					function(p) {
// 						that._canLocate = true;
// 						var pos = new G.LatLng(p.coords.latitude,p.coords.longitude);
// 						that.marker.setPosition(pos);
// 						if (that.track && that.map) {
// 							that.map.setOptions({draggable:false});
// 							that.map.panTo(pos);
// 						}
// 						for (var l in that.listeners) that.listeners[l](pos);
// 					},
// 					function(e) {
// 						//Error: No s'ha pogut geolocalitzar (per exemple: no hi ha cobertura gps)
// 						that._canLocate = false;
// 						that._goError(e);
// 					},
// 					{ enableHighAccuracy:true }
// 				);
// 			},
// 			function(e) {
// 				//Error: L'usuari no permet la geolocalitzacio
// 				that._canLocate = false;
// 				that._goError();
// 			},
// 			{ enableHighAccuracy:true }
// 		);
// 	}
// }
// 
// geoloc.prototype.addListener = function(callback) {
// 	return this.listeners.push(callback);
// }
// 
// geoloc.prototype.removeListener = function(id) {
// 	this.listeners.splice(id,1)
// }
// 
// // Afegit un timeoutCallback que passat un temps salta a error
// // per evitar que es pengi el programa
// geoloc.prototype.getLocation = function(callback,errorCallback,timeoutCallback,timeout) {
// 	if (!this.marker) { //No tenim ni una posicio
// 		var that = this;
// 		if (typeof(callback) == "function") this.sucessCallback.push(callback);
// 		if (typeof(errorCallback) == "function") this.errorCallback.push(errorCallback);
// 		if (this.timeout) clearTimeout(this.timeout);
// 		this.timeout = setTimeout(function(){
// 			if(typeof(timeoutCallback) == "function") timeoutCallback();
// 			else{
// 				that._goError();
// 				that._cleanSuccess();
// 			}
// 		},(timeout || this._defaultTimeout));
// 		return this._try();
// 	} else { //Retorna la ultima posicio
// 		var pos = this.marker.getPosition();
// 		if (typeof callback == 'function') callback(pos); 
// 		return pos;
// 	}
// }
// geoloc.prototype._goError = function(e) {
// 	clearTimeout(this.timeout);
// 	for(var callb in this.errorCallback){ this.errorCallback[callb](e);}
// 	this.errorCallback = [];
// }
// geoloc.prototype._cleanSuccess = function() {
// 	clearTimeout(this.timeout);
// 	this.sucessCallback = [];
// }
// geoloc.prototype._try = function() {
// 	if(!navigator.geolocation){
// 		this._canLocate = false;
// 		this._goError();
// 		return false;
// 	}
// 	var that = this;
// 	navigator.geolocation.getCurrentPosition( //Fa que li aparegui a l'usuari si permet geolocalitzar
// 		function(p) {
// 			that._canLocate = true;
// 			var pos = new G.LatLng(p.coords.latitude,p.coords.longitude);
// 			for(var callb in that.sucessCallback) that.sucessCallback[callb](pos);
// 			that._cleanSuccess();
// 		},
// 		function(e) {
// 			//Error: No s'ha pogut geolocalitzar (per exemple: no hi ha cobertura gps)
// 			that._canLocate = false;
// 			that._goError(e);
// 		},
// 		{ enableHighAccuracy:true }
// 	);
// }
// 
// geoloc.prototype.getTracking = function() {
// 	return this.track;
// }
// 
// geoloc.prototype.disableTracking = function() {
//  	if (this.track) this.toggleTracking(true);
// }
// 
// geoloc.prototype.enableTracking = function() {
//  	if (!this.track) this.toggleTracking(true);
// }
// 
// geoloc.prototype.toggleTracking = function(silent) {
// 	var toastText;
// 	if (!this.track) { //Activar seguiment
// 		if (!this.marker) {
// 			if (silent) return;
// 			alert("@@Primer has de permetre la geolocalitzacio al teu navegador@@");
// 			return;
// 		} else {
// 			this.map.setZoom(16);
// 			this.map.panTo(this.marker.getPosition());
// 			var that = this;
// 			this.listener = listen(this.map,'zoom_changed',function() {
// 				that.map.panTo(that.marker.getPosition());
// 			});
// 			this.map.setOptions({draggable:false});
// 			this.track = true;
// 			if (this.button) this.button.className = 'view_control_button location_on_button';
// 			toastText = "@@Centrat automàtic activat@@";
// 		}
// 	} else { //Desactivar seguiment
// 		this.track = false;
// 		unlisten(this.listener);
// 		this.map.setOptions({draggable:true});
// 		if (this.button) this.button.className = 'view_control_button location_off_button'
// 		toastText = "@@Centrat automàtic desactivat@@";
// 	}
// 	if (silent) return;
// 	this.showToast(toastText);
// }
// 			
// geoloc.prototype.getButton = function() {
// 	var that = this;
// 	this.button = create('div');
// 	this.button.title = "@@Localitzar@@";
// 	this.button.className = 'view_control_button location_'+(this.track?'on':'off')+'_button';
// 	listen(this.button,'click',function(){
// 		if (navigator.geolocation) that.toggleTracking();
// 		else alert("@@El teu navegador no suporta geolocalització o no l'has permès@@");
// 	});
// 	return this.button;
// }
// 
// geoloc.prototype.canLocate = function() {
// 	return navigator.geolocation && this._canLocate;
// }